【数据结构与算法】双向链表-维吉尼亚加密


#include <stdio.h>
#include <string.h> 
#define ERROR 0
#define OK 1 
#define ElemType char
#define N 26
typedef int status;
typedef struct Node
{
	ElemType data;
	struct Node* perior;
	struct Node* next;
}Node;
typedef struct Node* Linklist;

/*遍历双向链表元素并打印*/ 
status VisitList(Linklist L)
{
   Linklist p;
   int j=0;
   p=(Linklist)malloc(sizeof(Node));
   p=L;
   while( p->next !=  L)
   {
   	 printf("%c",p->data);
   	 p=p->next;
   	 j++;
   }
   printf("%c\n",p->data);
   j++;	
   printf("链表长度为:%d\n",j);
   return OK;	
}
/*使用尾插法创建26个字母的双向链表*/ 
Linklist CreateListTail(Linklist *L)
{
	Linklist r,p,head;
	int j;
	*L=(Linklist)malloc(sizeof(Node));
	head=(Linklist)malloc(sizeof(Node));
	r=(Linklist)malloc(sizeof(Node));
    head=r=*L;
	srand(time(0)); 
	for(j=0;j<26;j++)
	{     
	    p=(Linklist)malloc(sizeof(Node));
	    p->data='A'+j;
		r->next=p;
		p->perior=r;
		r=p;
	}	
	r->next=head->next;
	head->next->perior=r;	
	free(head);	
	return r->next;
}
/*得到明文字母在链表中的位置*/ 
Linklist GetElem(Linklist L,ElemType e)
{
	Linklist p;
	p=L;
	int j=0;
	while( p->data !=  e)
	{
	   p=p->next;
       j++;	
	}
	return p;
}
/*根据随机生成数字进行移位*/ 
Linklist MoveList(Linklist *L,int n)
{
	Linklist head;
	int j;
	head=(Linklist)malloc(sizeof(Node));
    head=*L;
	if(n==0)
	return *L;
	if(n>0)
	{
	  for(j=0;j<n;j++)
	     head=head->next;		
	}
	if(n<0)
	{
	  for(j=0;j<-n;j++)
	     head=head->perior;		
	}	
	return head;
}

int main()
 {	
     int x,n;
     ElemType e; 
	 Linklist L,temp;
	 char s[1000];
	 int a[1000];
	 srand(time(0)); 
     FILE *fp;
	 fp=fopen("D:\\test.txt","w");
     L=CreateListTail(&L);
     printf("输入明文:");			
     scanf("%s",s);  
    for(x=0;x<strlen(s);x++)  
    {   temp=L;  
        a[x]=rand()%100;  
        temp=GetElem(L,s[x]);  
        temp=MoveList(&temp,a[x]%26);  
        printf("%c ",temp->data);  
        fprintf(fp,"%c ",temp->data);  
    }  
    printf("\n");   
    for(x=0;x<strlen(s);x++)  
    {     
         printf("%d ",a[x]);  
    }  

    fprintf(fp,"\n");  
    x=0;  
    n=strlen(s);  
     while(n--)  
     {fprintf(fp,"%d ",a[x]);  
       x++;  
     };  
    fclose(fp);  
 
  return 0;
 }


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值